<?php
namespace App\Http\Controllers\UserFollow\V10;

namespace App\Http\Controllers\UserFollow\V10;
use App\Constants\RedisKey;
use App\Http\Controllers\Controller;
use App\Constant\UserFollowTypeCode;
use App\Http\Requests\UserFollow\V10\UserFollowRequest;
use Illuminate\Support\Facades\Redis;

/**
 * Class UserFollowController
 */
class UserFollowController extends Controller {

    public $user;

    private $version = 'v10';

    public function __construct()
    {
        $this->user = auth('api')->user();
        $this->middleware('auth:api', ['only' => 'store']);
    }

    /**
     * Description: 埋点记录
     * Author: cg
     * DateTime: 2018-3-6 3:00
     * @param UserFollowRequest $request
     * @return mixed
     */
    public function store(UserFollowRequest $request)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $finger_print = $request->header('Finger-Print');
        try{
            $user = $this->user;
            $params['user_id'] = is_object($user) ? $user->id : 0;
            $params['finger_print'] = $finger_print ?? '';
            $params['ip'] = $request->getClientIp();
            $params['version'] = $this->version;
            $params['mac_address'] = $params['mac_address'] ?? '';
            $params['response'] = !empty($params['response']) ? $params['response'] : '';
            if(is_object($user) && !empty($user->source)){
                $params['channel'] = $user->source;
            }else{
                $params['channel'] = 0;
            }
            if(!array_key_exists('type_desc', $params) && empty($params['type_desc'])){
                $params['type_desc'] = UserFollowTypeCode::USER_FOLLOW_TYPE[intval($params['type'])];
            }
            $params['created_at'] = date("Y-m-d H:i:s", time());
            $json = json_encode($params, JSON_UNESCAPED_UNICODE);
            $key = RedisKey::USER_FOLLOW;
            Redis::lPush($key, $json);
        }catch (\Exception $e){
            return failed($e->getMessage());
        }
        return success([]);
    }

    /**
     * Description: 开放部分请求
     * Author: cg
     * DateTime: 2018-3-19 2:00
     * @param UserFollowRequest $request
     * @return mixed
     */
    public function store_two(UserFollowRequest $request)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        $finger_print = $request->header('Finger-Print');
        try{
            $params['user_id'] = $params['user_id'] ?? 0;
            $params['finger_print'] = $finger_print ?? '';
            $params['ip'] = $request->getClientIp();
            $params['version'] = $this->version;
            $params['mac_address'] = $params['mac_address'] ?? '';
            $params['response'] = !empty($params['response']) ? $params['response'] : '';
            if(!array_key_exists('channel', $params)){
                $params['channel'] = 0;
            }
            if(!array_key_exists('type_desc', $params) && empty($params['type_desc'])){
                $params['type_desc'] = UserFollowTypeCode::USER_FOLLOW_TYPE[intval($params['type'])];
            }
            $params['created_at'] = date("Y-m-d H:i:s", time());
            $json = json_encode($params, JSON_UNESCAPED_UNICODE);
            $key = RedisKey::USER_FOLLOW;
            Redis::lPush($key, $json);
        }catch (\Exception $e){
            return failed($e->getMessage());
        }
        return success([]);
    }
}